<?php

namespace App\Dao;

use App\Model\CategoryModel;

class CategoryDao extends BaseDao
{
    public static function get_category_builder($params = []){
        $query = CategoryModel::query();

        $query->when(checkIsHasField($params,'category_id'),function ($query)use ($params){
            $query->where('category_id',$params['category_id']);
        });

        return $query;
    }

    public static function add($params = []){
        if(self::get_category_builder()->create($params) === false){
            self::error('添加失败');
        }
    }

    public static function update($params = []){
        if(self::get_category_builder(['category_id'=>self::deleteParamId($params,'category_id')])->update(self::filterFields($params,new CategoryModel())) === false){
            self::error('修改失败');
        }
    }

    public static function get_category_page(array $all)
    {
        return self::get_category_builder($all)->paginate();
    }

    public static function get_one(array $all)
    {
        return self::get_category_builder($all)->first();
    }

    public static function get_datas(){
        return self::get_category_builder()->get();
    }
}
